/**
 * 
 */
package com.effectiv.gooruda.mvc.controller;


import java.util.List;


import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

import com.effectiv.gooruda.domain.ServerDetail;
import com.effectiv.gooruda.repository.ServerDetailRepository;


/**
 * @author Dhrubo
 * 
 */
@Controller
@Slf4j
public class DocsController {
	
	@Autowired
	private ServerDetailRepository repository;
	
	
	@RequestMapping(value = "/docs")
	public ModelAndView docs(@RequestParam("a") String app, ModelAndView mv){
		log.info("Loading documentation for app = {}", app);
		
		List<ServerDetail> servers =  repository.findByContext(app);
		
		log.info("Servers = {}", servers);
		
		String url = null;
		
		if(servers != null){
			//get the first one and move on
			ServerDetail sd = servers.get(0);
			url = "http://" + sd.getName() + ":" + sd.getPort() + "/" + sd.getContext() + "/apidocs";
			
			log.info("URL = {}", url);
		}
		
				
		mv.addObject("url", url);
		mv.setViewName("jsondoc");
		
		return mv;
	}
	
	
	
	
}
